package com.mz.ozy.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.activity.assets.ExchangeActivity;
import com.mz.ozy.activity.home.ExchangeDetailActivity;
import com.mz.ozy.activity.home.HomeActivity1;
import com.mz.ozy.activity.home.ReciprocityActivity;
import com.mz.ozy.activity.shop.ShopDetailActivity;
import com.mz.ozy.adapter.ExChangeAdapter;
import com.mz.ozy.adapter.RecyclerViewAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.MyCardBean;
import com.mz.ozy.mvp.presenter.MyCardPresenter;
import com.mz.ozy.mvp.view.MycardContract;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lifeStyle on 2020/4/3 0003.
 */
public class MyCardActivity extends BaseActivity<MycardContract.View, MyCardPresenter> implements MycardContract.View {

    @BindView(R.id.tv_one)
    TextView tvOne;
    @BindView(R.id.view_one)
    View viewOne;
    @BindView(R.id.tv_two)
    TextView tvTwo;
    @BindView(R.id.view_two)
    View viewTwo;
    @BindView(R.id.tv_three)
    TextView tvThree;
    @BindView(R.id.view_three)
    View viewThree;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.image_data)
    ImageView image_data;
    @BindView(R.id.tv_nota)
    TextView tv_nota;
    @BindView(R.id.iamge)
    ImageView iamge;

    private int pageNo = 1;
    private int tag = 0;
    private int type = 1;
    private List<MyCardBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();
    private ExChangeAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_card);
        ButterKnife.bind(this);
        initTitle();
        init();
        image_data.setImageResource(R.mipmap.card_no_data);
        initView();
        tv_nota.setText("暂无兑换券");
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshCardList();
    }

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ExChangeAdapter(this, mBeanList, type);
        recyclerview.setAdapter(mAdapter);
        smartRefreshLayout.setEnableAutoLoadmore(true);
//        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getInfo(MyCardActivity.this, pageNo + "", type + "");
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                refreshCardList();
            }
        });
        mAdapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, Object dataItem) {
                final MyCardBean.PageBean.PageItemsBean itembean = (MyCardBean.PageBean.PageItemsBean) dataItem;
                if (itembean.getState() == 1) {
//                    Intent intent = new Intent(MyCardActivity.this, ExchangeDetailActivity.class);
//                    intent.putExtra("goodsId", itembean.getProductid());
//                    startActivity(intent);
                }
            }

            @Override
            public void onItemLongClick(View view, int position, Object dataItem) {

            }
        });
    }

    private void refreshCardList() {
        tag = 0;
        pageNo = 1;
        mPresenter.getInfo(MyCardActivity.this, pageNo + "", type + "");
    }

    private void init() {
        tvCenterTitle.setText("我的卡券");
        rightTextView.setTextSize(12);
        rightTextView.setGravity(Gravity.CENTER);
        rightTextView.setTextColor(getResources().getColor(R.color.white));
        rightTextView.setVisibility(View.VISIBLE);
        rightTextView.setText("兑换卡券");
        rightTextView.setBackgroundResource(R.drawable.round_73b21a);
        rightTextView.setHeight(70);
        rightTextView.setWidth(160);
        rightTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyCardActivity.this, ExchangeActivity.class));
            }
        });
    }


    private void initTab(TextView one, TextView two, TextView three, View viewOne, View viewTwo, View viewThree) {
        one.setTextColor(getResources().getColor(R.color.c_363636));
        one.setTextSize(15);
        viewOne.setVisibility(View.VISIBLE);
        two.setTextColor(getResources().getColor(R.color.c_a5a5a5));
        two.setTextSize(12);
        viewTwo.setVisibility(View.INVISIBLE);
        three.setTextColor(getResources().getColor(R.color.c_a5a5a5));
        three.setTextSize(12);
        viewThree.setVisibility(View.INVISIBLE);
    }

    @OnClick({R.id.ll_one, R.id.ll_two, R.id.ll_three})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_one:
                initTab(tvOne, tvTwo, tvThree, viewOne, viewTwo, viewThree);
                type = 1;
                refreshCardList();
                break;
            case R.id.ll_two:
                type = 2;
                initTab(tvTwo, tvOne, tvThree, viewTwo, viewOne, viewThree);
                refreshCardList();
                break;
            case R.id.ll_three:
                type = 3;
                initTab(tvThree, tvTwo, tvOne, viewThree, viewTwo, viewOne);
                refreshCardList();
                break;
        }
    }

    @Override
    public void getInfoSuccess(MyCardBean bean) {
        GlideUtils.loadImg(this, iamge, bean.getVipimg(), R.mipmap.bg_card_empty);
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        mAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    @Override
    public void getAccountSuccess(JSONObject bean) {

    }
}
